package com.kmvc.message.tools;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetWorkTool {
	
	 public static String getContent(String url) throws Exception{
		  StringBuilder sb = new StringBuilder(); 
		  
		  HttpClient client = new DefaultHttpClient(); 
		  HttpParams httpParams = client.getParams(); 
		  //设置网络超时参数        
		  HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
		  HttpConnectionParams.setSoTimeout(httpParams, 5000);       
		  HttpResponse response = client.execute(new HttpGet(url)); 
		  HttpEntity entity = response.getEntity(); 
		  if (entity != null) {   
			  BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "GBK"), 8192); 
			  String line = null;     
			  while ((line = reader.readLine())!= null){  
				  sb.append(line);             
				  }                 
			  reader.close();      
			  }             
		  return sb.toString();        
		  }
	  
	
	public boolean isNetwork(Context context)
	{
		boolean isnetwork=false;
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkinfo = manager.getActiveNetworkInfo();
		if (networkinfo == null || !networkinfo.isAvailable()) {
			isnetwork =false;
		} else {
			int type = networkinfo.getType();
			if (type == ConnectivityManager.TYPE_MOBILE
					|| type == ConnectivityManager.TYPE_WIFI) {
				isnetwork= true;

			}
		}
		return isnetwork;
	}

}
